package com.n.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.n.application.SystemCache;
import com.n.common.BaseAction;
import com.n.dao.CustomerBillRecordDao;
import com.n.dao.CustomerDao;
import com.n.model.Customer;
import com.n.model.CustomerBillRecord;
import com.n.util.Constant;
import com.n.util.DateUtil;
import com.n.vo.YMBillRec;
@Controller
@RequestMapping("/cbill")
public class CustomerBillAction extends BaseAction {
	static final String PRE_PATH = "/admin/cbill/";
	
	@Autowired
	CustomerDao customerDao;
	
	@Autowired
	CustomerBillRecordDao customerBillRecordDao;
	
	
	
	@RequestMapping(value="save.do")
	@ResponseBody
	public String save(HttpServletRequest request) throws Exception{
		String sid = request.getParameter("id");
		String scId = request.getParameter("customerId");
		CustomerBillRecord c = new CustomerBillRecord();
		c.setCustomerId(Integer.parseInt(scId));
		c.setDate(DateUtil.parseStrToDate(request.getParameter("date")));
		c.setType(Integer.parseInt(request.getParameter("type")));
		c.setMoney(Double.parseDouble(request.getParameter("money")));
		c.setRemark(request.getParameter("remark"));
		c.setOptDate(new Date());
		c.setOptUser("");
		if(!StringUtils.isEmpty(sid)){
			c.setId(Integer.parseInt(sid));
		}
		if(c.getType() == Constant.BILL_TYPE_HK){
			c.setPayType(Integer.parseInt(request.getParameter("payType")));
		}
		customerBillRecordDao.merge(c);
		return SUCCESS;
	}
	
	
	@RequestMapping(value="list.do")
	public String list(HttpServletRequest request, ModelMap model){
		request.getSession().setAttribute("customerList", SystemCache.CUSTOMER_LIST);
		model.put("year", DateUtil.getCurrentYear());
		model.put("month", DateUtil.getCurrentMonth());
		return PRE_PATH + "index";
	}
	
	
	@RequestMapping(value="load.do")
	@ResponseBody
	public String load(HttpServletRequest request) throws Exception{
		String sid = request.getParameter("id");
		if(StringUtils.isEmpty(sid)){
			return ERROR;
		}
		long id = Long.parseLong(sid);			
		CustomerBillRecord c = customerBillRecordDao.load(CustomerBillRecord.class,id);
		JSONObject json = JSONObject.fromObject(c);
		json.put("dateStr", DateUtil.parseDateToStr(c.getDate()));
		json.put("success", SUCCESS);
		System.out.println(json);
		return json.toString();
	}
	
	
	@RequestMapping(value="loadCustomerBill.do")
	public String loadCustomerBill(HttpServletRequest request, ModelMap model){
		
		String sid = request.getParameter("id");
		if(StringUtils.isEmpty(sid)){
			return ERROR;
		}
		int customerId = Integer.parseInt(sid);
		
		String m = request.getParameter("m");
		if(StringUtils.isEmpty(m)){
			m = DateUtil.getCurrentMonth()+"";
		}
		int month = Integer.parseInt(m);
		
		String y = request.getParameter("y");
		if(StringUtils.isEmpty(y)){
			y = DateUtil.getCurrentYear()+"";
		}
		int year =  Integer.parseInt(y);
		
		
		Customer customer = customerDao.load(Customer.class, customerId);
		List<CustomerBillRecord> cbills = (List<CustomerBillRecord>)customerBillRecordDao.loadCustomerYMBill(customerId,year,month);
		Map<String,Double> moneyMap = countMoney(cbills);
		Map<Integer,Double> typeMoneyMap = customerBillRecordDao.countAll(customerId);
		
		model.put("customerId", customer.getId());
		model.put("customerName", customer.getName());
		
		model.put(Constant.KEY_YEAR, year);
		model.put(Constant.KEY_MONTH, month);
		
		model.put(Constant.KEY_MONTH_QK, moneyMap.get(Constant.KEY_MONTH_QK));
		model.put(Constant.KEY_MONTH_HK, moneyMap.get(Constant.KEY_MONTH_HK));
		model.put(Constant.KEY_ALL_QK, typeMoneyMap.get(Constant.BILL_TYPE_QK));
		model.put(Constant.KEY_ALL_HK, typeMoneyMap.get(Constant.BILL_TYPE_HK));
		
		model.put("cbills_data", cbills);
		return PRE_PATH + "index";
	}
	
	@RequestMapping(value="del.do")
	@ResponseBody
	public String del(HttpServletRequest request){
		String sid = request.getParameter("id");
		if(StringUtils.isEmpty(sid)){
			return ERROR;
		}
		int id = Integer.parseInt(sid);
		customerBillRecordDao.del(id);
		return SUCCESS;
	}
	
	public Map<String,Double> countMoney(List<CustomerBillRecord> cbills){
		Map<String,Double> moneyMap = new HashMap<String, Double>();
		double qiankuan = 0;
		double haikuan = 0;
		for (CustomerBillRecord rec : cbills) {
			if(rec.getType() == Constant.BILL_TYPE_QK)
				qiankuan += rec.getMoney();
			if(rec.getType() == Constant.BILL_TYPE_HK)
				haikuan += rec.getMoney();
		}
		moneyMap.put(Constant.KEY_MONTH_QK, qiankuan);
		moneyMap.put(Constant.KEY_MONTH_HK, haikuan);
		return moneyMap;
	}
	
	
	
	
	
	
	//---------------------------------------------------------------------------------------------------------------
	
	/**
	 * 客户账单汇总统计
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="countlist.do")
	public String countlist(HttpServletRequest request, ModelMap model){
		request.getSession().setAttribute("customerList", SystemCache.CUSTOMER_LIST);
		String y = request.getParameter("y");
		if(StringUtils.isEmpty(y)){
			y = DateUtil.getCurrentYear()+"";
		}
		int year =  Integer.parseInt(y);
		model.put(Constant.KEY_YEAR, year);
		List<YMBillRec> data = customerBillRecordDao.countList(year);
		model.put("data", data);	
		return PRE_PATH + "countlist";
	}

}
